/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-28     Titan       the first version
 */

#include "ledblink_user.h"

/*板载灯*/
#define LED_GRE_BOARD   GET_PIN(B, 14)
#define LED_RED_BOARD   GET_PIN(B, 13)
/*面贴灯*/
#define LED_COM     GET_PIN(B, 11)
#define LED_ERR     GET_PIN(E, 15)
#define LED_RUN     GET_PIN(E, 13)
#define LED1_FUNC   GET_PIN(E, 11)
#define LED2_L_SIN  GET_PIN(E, 9)
#define LED3_R_SIN  GET_PIN(E, 7)
#define LED4_REHOME GET_PIN(B, 1)
#define LED5_L      GET_PIN(B, 0)
#define LED6_R      GET_PIN(C, 5)

void bled_on(enum LEDUSER led){
    if(led >= BLED_MAX){
        return;
    }
    led_on(led);
}

void bled_off(enum LEDUSER led){
    if(led >= BLED_MAX){
        return;
    }
    led_off(led);
}

void bled_delay(enum LEDUSER led, int time){
    if(led >= BLED_MAX || time <= 0){
        return;
    }
    led_blink(led,time,0);
}

void bled_blink(enum LEDUSER led, int ontime, int offtime){
    if(led >= BLED_MAX){
        return;
    }
    led_blink(led,ontime,offtime);
}

static void bled_scan(void *arg)
{
    while(1)
    {
        led_process(1);
        rt_thread_mdelay(1);
    }
}
void user_bled_init(void){
    led_add_device(LED1_FUNC);
    led_add_device(LED2_L_SIN);
    led_add_device(LED3_R_SIN);
    led_add_device(LED4_REHOME);
    led_add_device(LED5_L);
    led_add_device(LED6_R);
    led_add_device(LED_RUN);
    led_add_device(LED_ERR);
    led_add_device(LED_COM);
    led_add_device(LED_RED_BOARD);
    led_add_device(LED_GRE_BOARD);
}
int bled_user_init(void)
{
    rt_thread_t tid = RT_NULL;

    user_bled_init();

    /* Create background ticks thread */
    tid = rt_thread_create("bled_user", bled_scan, RT_NULL, 1024, 10, 10);
    if(tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }

    rt_thread_mdelay(10);
    for(int i=(BLED_MAX-1);i>0;i--){
        bled_off(i);
    }
    return 0;
}
INIT_APP_EXPORT(bled_user_init);

